package com.abbas.abchat.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.abbas.abchat.AbChatApplication;
import com.abbas.abchat.R;
import com.abbas.abchat.activity.SearchAccountActivity;
import com.abbas.abchat.activity.SplashActivity;
import com.abbas.abchat.activity.UserInfoActivity;
import com.abbas.abchat.adapter.AccountListAdapter;
import com.abbas.abchat.adapter.ChatAdapter;
import com.abbas.abchat.adapter.ItemRecycleAdapter;
import com.abbas.abchat.adapter.OnRecycleViewItemClickListener;
import com.abbas.abchat.adapter.SearchAccountListAdapter;
import com.abbas.abchat.dao.AccountDao;
import com.abbas.abchat.dao.ChatTableDao;
import com.abbas.abchat.dao.impl.AccountDaoImpl;
import com.abbas.abchat.dao.impl.ChatTableDaoImpl;
import com.abbas.abchat.entity.Account;
import com.abbas.abchat.entity.ChatTable;
import com.abbas.abchat.entity.Item;
import com.abbas.abchat.utils.SpUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MineFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MineFragment extends Fragment implements View.OnClickListener{

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private View mView;
    private LinearLayout llMine;
    private ImageView img;
    private TextView name;
    private TextView tvChatId;
    private TextView tvStatus;
    private RecyclerView rv;

    private ItemRecycleAdapter itemRecycleAdapter;


    public MineFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MineFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MineFragment newInstance(String param1, String param2) {
        MineFragment fragment = new MineFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (mView != null){
            ViewGroup parent = (ViewGroup) mView.getParent();
            if (parent != null){
                parent.removeView(mView);
            }
            return mView;
        }
        mView = inflater.inflate(R.layout.fragment_mine, container, false);

        llMine = (LinearLayout) mView.findViewById(R.id.ll_mine);
        img = (ImageView) mView.findViewById(R.id.img);
        name = (TextView) mView.findViewById(R.id.name);
        tvChatId = (TextView) mView.findViewById(R.id.tv_chatId);
        tvStatus = (TextView) mView.findViewById(R.id.tv_status);
        rv = (RecyclerView) mView.findViewById(R.id.rv);

        List<Item> items = new ArrayList<>();
        items.add(new Item(R.mipmap.ic_launcher,"服务"));
        items.add(new Item(R.mipmap.ic_launcher,"设置"));

        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.top = 8;
                super.getItemOffsets(outRect, view, parent, state);

            }
        });
        itemRecycleAdapter = new ItemRecycleAdapter(getActivity(), items);
        rv.setAdapter(itemRecycleAdapter);

        init();
        initEvent();

        return mView;
    }

    private void init() {
        Account account = AbChatApplication.getAccount();

        if (!TextUtils.isEmpty(account.getImgPath())){
            Glide.with(getActivity())
                    .load(account.getImgPath())
                    .transform(new RoundedCorners(14))
                    .into(img);
        }else {
            Glide.with(getActivity())
                    .load(R.mipmap.ic_launcher)
                    .transform(new RoundedCorners(14))
                    .into(img);
        }
        if (!TextUtils.isEmpty(account.getName())){
            name.setText(account.getName());
        }else {
            name.setText("暂无名称");
        }

        tvChatId.setText("账号: "+account.getAccount());


    }

    private void initEvent() {

        itemRecycleAdapter.setItemClickListener( new OnRecycleViewItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder, int position) {
                switch (position){
                    case 0:
                        break;
                    case 1:
                        AlertDialog dialog = new AlertDialog.Builder(getActivity())
                                .setTitle("退出登录")
                                .setMessage("点击确定退出登录")
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                })
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        SpUtils.config(getActivity(),SpUtils.LOGIN_ACCOUNT).clear();
                                        startActivity(new Intent(getActivity(), SplashActivity.class));
                                        dialog.dismiss();
                                        getActivity().finish();
                                    }
                                })
                                .create();
                        dialog.show();
                        break;
                }
            }
        });

        llMine.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        init();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_mine:
                startActivity(new Intent(getActivity(), UserInfoActivity.class));
                break;
        }
    }

    static class ItemDecoration extends RecyclerView.ItemDecoration{

        public ItemDecoration() {
            super();
        }

        @Override
        public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.onDraw(c, parent, state);
        }

        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
        }
    }
}